/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ULHT.LIG2N1.RC.VO;

import ULHT.LIG2N1.RC.SERVER.ClientGameData;
import ULHT.LIG2N1.RC.SERVER.GameVariables;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 *
 * VO com o log do jogo
 */
public class GameLogVO implements Serializable{

    private ArrayList<RoundLogVO> logJogo = new ArrayList<>();
    private GameVariables gameVariables;
    private HashMap<String, ClientGameData > clientes;
    private final String filename = "Log.Data";

    public GameLogVO() {
    }
    
    public String getFilename() {
        return filename;
    }
   
    public GameLogVO(GameVariables gameVariables) {
        this.gameVariables = gameVariables;
        this.clientes = gameVariables.getClients();
    }

    public void setGameVariables(GameVariables gameVariables) {
        this.gameVariables = gameVariables;
    }

    public GameVariables getGameVariables() {
        return gameVariables;
    }
    
    //TODO iterador sobre o objecto gameVariables e preenche os arrays
    private void updateRoundStats(int round) {
        RoundLogVO ronda = new RoundLogVO(round);
        //Iterar hashmap
        Iterator<ClientGameData> it = clientes.values().iterator();
        while (it.hasNext()) {
            ClientGameData clientGameData = it.next();

            if (clientGameData.getLastQuestionAnswered() == round) {
                ronda.autoAdd(clientGameData.getToken(), clientGameData.getIpAdress(), clientGameData.getLastAnswerTime(), clientGameData.isLastAnswerCorrect());
            }
        }
    }
    
    public ArrayList<GameLogVO> loadGameLogVO() throws FileNotFoundException, IOException{
        FileManage fileManage = new FileManage();
        return fileManage.readGameLogFromFile(this.filename);
    }
    
    public void writeGameLogVO() throws FileNotFoundException, IOException{
        FileManage fileManage = new FileManage();
        fileManage.writeGameLogToFile(this, this.filename); 
    }
}
